﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace Study_Time
{
    static class Program
    {
        private static StudyTimeForm studyTimeForm;

        public enum StudyTimeMode { Study, Edit };
        private static StudyTimeMode mode;
        public static StudyTimeMode Mode
        {
            get
            {
                return mode;
            }
            set
            {
                // Reset the study control before changing the mode.
                // Otherwise the buttons see that it is in edit mode, and then save the current values.
                studyTimeForm.ResetStudyControl();

                mode = value;

                studyTimeForm.UpdateMode();
            }
        }

        static Boolean IsAlreadyRunning()
        {
            Boolean canRun = false;

            System.Threading.Mutex mutex = new System.Threading.Mutex(true, "MeanBeanStudyTime", out canRun);

            if (!canRun)
                return true;
            else
                return false;
        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(String[] args)
        {
            // Check if already running
            if (IsAlreadyRunning())
            {
                MessageBox.Show("Another instance is already running.");
                return;
            }

            // Application startup
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            // Create the study time form
            studyTimeForm = new StudyTimeForm();
            
            // Set the current mode
            Mode = StudyTimeMode.Study;

            // Load the stf if there is an argument
            if (args.Length > 0)
            {
                if (args[0].EndsWith(".stf"))
                    studyTimeForm.LoadStf(args[0]);
            }

            // Show the form
            Application.Run(studyTimeForm);
        }
    }
}
